package org.example.controller;

import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author CoderXshuai
 * @date 2020/9/4/0004 15:36
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(@RequestParam String username, @RequestParam String psw) {
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.login(username, psw);
        modelAndView.addObject(user);
        modelAndView.setViewName("redirect:/main_admin.html");
        return modelAndView;
    }

    @RequestMapping("/register")
    public ModelAndView register(@RequestParam String username, @RequestParam String psw) {
        User user = new User();
        user.setUsername(username);
        user.setPsw(psw);
        userService.insertUser(user);
        ModelAndView mv = new ModelAndView("redirect:/main_admin.html");
        mv.addObject(user);
        return mv;
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> findAll() {
        return userService.findAllUser();
    }

    @RequestMapping("/deleteEquipment")
    public void deleteEquipment(@RequestParam List<Integer> uidList) {
        for (Integer uid : uidList) {
            userService.deleteUser(uid);
        }
    }


}
